﻿using System;
using Validate.Net.Core;

namespace Validate.Net.Internals
{
    /// <summary>
    /// 属性必填
    /// </summary>
    public class RequiredAttribute : ValidateBaseAttribute
    {
        /// <summary>
        /// 属性必填
        /// <param name="internalPrompts">内联提示语</param>
        /// </summary>
        public RequiredAttribute(string internalPrompts = null) : base(internalPrompts)
        {

        }

        /// <summary>
        /// 如果属性是字符串，则是否跳过对空白字符串的校验。
        /// <para>默认值：false [校验空白字符串]</para>
        /// </summary>
        public bool SkipWhiteSpace { get; set; } = false;

        public override string Prompts => Properties.Resources.RequiredPrompts;

        public override bool OnCheck(object value)
        {
            if (value is string str)
            {
                if (SkipWhiteSpace)
                { return !string.IsNullOrEmpty(str); }
                else { return !string.IsNullOrWhiteSpace(str); }
            }
            else
            { return value != null; }
        }
    }
}
